import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {AlertComponent} from "../../unit/alert";
import {PhoneVerification} from "../../modals/basic.modal";
import {BasicService} from "../../unit/basic-service";

@Component({
    selector: 'app-reset-password',
    templateUrl: './reset-password.page.html',
    styleUrls: ['../login/login.page.scss', './reset-password.page.scss'],
})
export class ResetPasswordPage implements OnInit {
    formData: PhoneVerification;
    currentType: string; // 1:短信认证改密码  2:密码认证改密码
    currentAccount: string; // 当前帐号
    confirmPassword: string; // 确认密码
    submitLoading: boolean;

    constructor(private route: Router,
                public alertComponent: AlertComponent,
                private activateInfo: ActivatedRoute,
                private basicService: BasicService
    ) {
    }

    ngOnInit() {
        this.formInit();
        this.activateInfo.queryParamMap.subscribe((res: any) => {
            this.initPage(res.params);
        });
    }

    initPage(params) {
        this.currentType = params.type;
        this.currentAccount = params.account;
    }

    submitData() {
        if (this.confirmPassword === this.formData.password) { // 确认密码
            const formData = {
                oldPassword: this.formData.oldPassword,
                newPassword: this.confirmPassword,
            };
            this.submitLoading = true;
            this.basicService.postJsonData('account/updatePassword',
                formData).subscribe(res => {
                if (res.code === 14014) {
                    // 修改成功后 重新登录
                    this.route.navigate([`/login`]).then();
                }
                this.alertComponent.presentAlert(res.text).then();
                this.submitLoading = false;
            });
        }
    }

    formInit() {
        this.formData = {
            password: '',
            oldPassword: '',
        }
    }

}
